import cv2
import numpy as np
if __name__ == '__main__':
    image_np = cv2.imread('work5.jpg')
    angle = 180
    scale = 1
    M = cv2.getRotationMatrix2D((0, 0), angle, scale)
    rotation_image = cv2.warpAffine(image_np, M, (image_np.shape[1], image_np.shape[0]), flags = cv2.INTER_LANCZOS4, borderMode= cv2.BORDER_WRAP)

    flip_image = cv2.flip(rotation_image,1, )

    x_min, x_max = 600, 1300
    y_min, y_max = 150,1200

    ROI_image = flip_image.copy()[y_min:y_max, x_min:x_max]
    line_width = 2
    cv2.rectangle(rotation_image, (x_min, y_min), (x_max, y_max), (0, 0, 255), line_width)

    points = [[207, 36], [545, 220], [100, 862], [497, 1011]]
    pts1 = np.float32(points)
    image_line = ROI_image.copy()
    cv2.line(image_line, points[0], points[1], (0, 0, 255), 2, cv2.LINE_AA)
    cv2.line(image_line, points[1], points[3], (0, 0, 255), 2, cv2.LINE_AA)
    cv2.line(image_line, points[3], points[2], (0, 0, 255), 2, cv2.LINE_AA)
    cv2.line(image_line, points[2], points[0], (0, 0, 255), 2, cv2.LINE_AA)

    img_shape = ROI_image.shape
    points = [[0, 0], [img_shape[1], 0], [0, img_shape[0]], [img_shape[1], img_shape[0]]]
    pts2 = np.float32(points)
    M = cv2.getPerspectiveTransform(pts1, pts2)
    correct_image = cv2.warpPerspective(ROI_image, M, (img_shape[1], img_shape[0]), cv2.INTER_LANCZOS4, cv2.BORDER_WRAP)

    # cv2.imshow('', rotation_image)
    cv2.imshow('', correct_image)
    cv2.imwrite('zuoye5.jpg', correct_image)
    cv2.waitKey(0)
